﻿using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

namespace LightGameLibs.Editor
{
    public static class EditorUtil
    {
        public static int WriteEnumToCS(string enumName, string[] enums, string sperateEnum, int sperateNum, string path, string addEnum = null, string namespaceName = null, bool canSerialize = false)
        {
            var start = string.IsNullOrEmpty(namespaceName) ? string.Empty
                : "namespace ".Add(namespaceName);
            start = canSerialize ? start.Add("\n{\n[System.Serializable]").Add("\npublic enum ").Add(enumName).Add("\n{\n")
                : start.Add("\n{\npublic enum ").Add(enumName).Add("\n{\n");
            var end = string.IsNullOrEmpty(namespaceName) ? "\n\n}" : "\n\n}\n}";
            var content = string.Empty;
            int j = 0;
            for (int i = 0, length = enums.Length; i < length; i++, j++)
            {
                if (string.IsNullOrEmpty(enums[i]))
                {
                    DebugMgr.Log(DebugTag.UTILITY, "EnumName Cant be Empty");
                    return i;
                }

                if (enums[i].Equals(sperateEnum))
                {
                    j = sperateNum;
                }

                content = StringUtility.Format("{0}\n{1} = {2},", content, enums[i], j.ToString());
            }
            content = string.IsNullOrEmpty(addEnum) ? StringUtility.Format("{0}{1}{2}", start, content, end)
                : StringUtility.Format("{0}{1}\n{2} = {3},{4}", start, content, addEnum, j.ToString(), end);

            System.IO.File.WriteAllText(path, content);
            AssetDatabase.Refresh();
            return j;
        }

        public static bool BrowsableAddress(string name, string defaultPath, string key, string end)
        {
            bool ret = false;
            string path = EditorPrefs.GetString(key, defaultPath);
            if (string.IsNullOrEmpty(path))
            {
                DebugMgr.LogWarning(DebugTag.UTILITY, "Cant found path");
                return false;
            }

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(StringUtility.Format("{0} Path:", name), path);
            if (GUILayout.Button("Browse"))
            {
                var defaultDirec = path.ToUpperFolder();
                var result = EditorUtility.OpenFilePanel("Select Enum Script", defaultDirec, end);
                if (!string.IsNullOrEmpty(result))
                {
                    EditorPrefs.SetString(key, result);
                    path = result;
                    ret = true;
                }
            }
            EditorGUILayout.EndHorizontal();

            return ret;
        }

        public static void WriteArrayToJson<T>(T[] array, string path)
        {
            string content = ArrayJsonUtility.ToJson<T>(array);
            System.IO.File.WriteAllText(path, content);
            AssetDatabase.Refresh();
        }

        public static void CombinableRichTextArgEditor(CombinableRTA target, int spaceNum = 0)
        {
            EditorGUILayout.BeginHorizontal();
            for (int i = 0, length = spaceNum; i < length; i++)
            {
                EditorGUILayout.Space();
            }
            EditorGUILayout.BeginVertical();
            target.m_color = EditorGUILayout.ColorField("TextColor:", target.m_color);
            target.m_size = EditorGUILayout.IntField("TextSize:", target.m_size);
            target.m_type = (FontStyle)EditorGUILayout.EnumPopup("TextType:", target.m_type);
            target.m_content = (LocalizationTag)EditorGUILayout.EnumPopup("Content:", target.m_content);
            EditorGUILayout.LabelField("Before:");
            RichTextArgEditor(target.m_before, spaceNum + 1);
            EditorGUILayout.LabelField("After:");
            RichTextArgEditor(target.m_after, spaceNum + 1);
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Filled:".Add(target.m_filled.Count.ToString()));
            if (GUILayout.Button("+"))
            {
                target.m_filled.Add(new RichTextArg());
            }
            EditorGUILayout.EndHorizontal();
            for (int i = 0, length = target.m_filled.Count; i < length; i++)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Pos:".Add(i.ToString()));
                if (GUILayout.Button("-"))
                {
                    target.m_filled.RemoveAt(i);
                    return;
                }
                EditorGUILayout.EndHorizontal();
                RichTextArgEditor(target.m_filled[i], spaceNum + 1);
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }

        public static void RichTextArgEditor(RichTextArg target, int spaceNum = 0)
        {
            EditorGUILayout.BeginHorizontal();
            for (int i = 0, length = spaceNum; i < length; i++)
            {
                EditorGUILayout.Space();
            }
            EditorGUILayout.BeginVertical();
            target.m_color = EditorGUILayout.ColorField("TextColor:", target.m_color);
            target.m_size = EditorGUILayout.IntField("TextSize:", target.m_size);
            target.m_type = (FontStyle)EditorGUILayout.EnumPopup("TextType:", target.m_type);
            target.m_content = (LocalizationTag)EditorGUILayout.EnumPopup("Content:", target.m_content);
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }
    }
}
